package top.ffhome.concurrent.beautiful.ch01;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

public class DeadLockTest {
    private static final Log log = LogFactory.get();
    private static Object resourceA = new Object();
    private static Object resourceB = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(() -> {
            synchronized (resourceA) {
                log.info("{} get ResourceA", Thread.currentThread());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    log.error(e);
                }
                log.info("{} waiting get ResourceB", Thread.currentThread());
                synchronized (resourceB) {
                    log.info("{} get ResourceB", Thread.currentThread());
                }
            }
        });

        Thread threadB = new Thread(() -> {
            synchronized (resourceB) {
                log.info("{} get ResourceB", Thread.currentThread());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    log.error(e);
                }
                log.info("{} waiting get ResourceA", Thread.currentThread());
                synchronized (resourceA) {
                    log.info("{} get ResourceA", Thread.currentThread());
                }
            }
        });

        threadA.start();
        threadB.start();
    }
}
